﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace TCDC_System.Content.TransectionData
{
    public partial class member_profile : System.Web.UI.Page
    {
        private Member_profileModel objMemberfile;
        private tcdc_mst_member_type objMemberType;
        private tcdc_mst_user objUser;
        private tcdc_trn_user_campaign objUserCampaign;
        private tcdc_mst_title objTitle;
        private tcdc_mst_site objSite;
        private tcdc_mst_address objAddress;
        
        
        protected void Page_Init(object sender, EventArgs e)
        {
          //  this.UserAdd1.GridStore = StoreUser;
        }

        protected void Page_Load(object sender, EventArgs e)
        {


            this.ImageMember.ImageUrl = "~/Attribute/images/userprofile/tav2.jpg";

            objMemberType = new tcdc_mst_member_type();
            objMemberType.strConn = ConfigurationManager.AppSettings["ConnString"];

            objMemberfile = new Member_profileModel();
            objMemberfile.strConn = ConfigurationManager.AppSettings["ConnString"];

            objUser = new tcdc_mst_user();
            objUser.strConn = ConfigurationManager.AppSettings["ConnString"];

            objUserCampaign = new tcdc_trn_user_campaign();
            objUserCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];

            objTitle=new tcdc_mst_title();
            objTitle.strConn = ConfigurationManager.AppSettings["ConnString"];

            objSite = new tcdc_mst_site();
            objSite.strConn = ConfigurationManager.AppSettings["ConnString"];

            objAddress = new tcdc_mst_address();
            objAddress.strConn = ConfigurationManager.AppSettings["ConnString"];

            string QueryString_user_campaign_id = Request.QueryString["user_campaign_id"];

            if (QueryString_user_campaign_id != "")
            {
                getUserCampaign(QueryString_user_campaign_id);
            }

            if (!X.IsAjaxRequest)
            {
                StoreTitleTH.DataSource =objTitle.getByObject(new tcdc_mst_title());
                StoreTitleTH.DataBind();

                StoreTitleEN.DataSource = objTitle.getByObject(new tcdc_mst_title());
                StoreTitleEN.DataBind();

                StoreSite_id.DataSource = objSite.getByObject(new tcdc_mst_site());
                StoreSite_id.DataBind();

                //StoreCampaign.DataSource = objMemberfile.getCampaign_Group();
                //StoreCampaign.DataBind();

                //getMemberGroup();
            }
        }

        private void getMemberGroup()
        {
            DataTable dt = objMemberfile.getCampaign_Group();
            this.StoreWindowCampaign.DataSource = dt;
            this.StoreWindowCampaign.DataBind();
        }

        protected void rowSelectCampaign(object sender, DirectEventArgs e)
        {
            getCampaignFee(txt_campaign_id.Text);
        }

        private void getCampaignFee(string cm_id)
        {
            DataTable dt = objMemberfile.getCampaign_Fee(cm_id);

            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Image Click",
                Message = cm_id
            });        

            //StoreFee
        }

        //protected void rowSelectCampaign(object sender, DirectEventArgs e)
        //{
        //    string strCampaign_id = e.ExtraParams["campaign_id"];
        //    txt_campaign_id.Text = strCampaign_id;
        //    WindowCampaign.Hide();
        //}

        protected void SelectMemberType(object sender, DirectEventArgs e)
        {
            //txt_campaign_id.Clear();
            //StoreCampaign.DataSource = objAddProfile.getCampaignByMemberType(txt_user_type_id.Text, "TH", "1");
            //StoreCampaign.DataBind();    
        }

        protected void SaveProfile(object sender, DirectEventArgs e)
        {
            Response.Redirect("pos.aspx?id=1234");
        }

        protected void RadioCheck(object sender, DirectEventArgs e)
        {
            WindowCampaign.Show();
        }

        protected void ClickSearch(object sender, DirectEventArgs e)
        {
            Member_profileModel i = new Member_profileModel();
            DataTable dtAddProfile = objMemberfile.getByObject(i);
        }

        protected void rowSelectMembers(object sender, DirectEventArgs e)
        {
            //user_id.Text = e.ExtraParams["user_id"];
            //getUserProfile(user_id.Text);
            //getUserCampaign(user_id.Text);
            //getAddress(user_id.Text);
            //getUserInformatoin(user_id.Text);
        }


        protected void getUserProfile(string my_user_id)
        {
            DataTable dtUser = objUser.getById(my_user_id);
            if (dtUser != null && dtUser.Rows.Count > 0)
            {
                txt_title_en.Text = dtUser.Rows[0]["title_en"].ToString();
                txt_fname_en.Text = dtUser.Rows[0]["fname_en"].ToString();
                txt_lname_en.Text = dtUser.Rows[0]["lname_en"].ToString();
                txt_title_th.Text = dtUser.Rows[0]["title_th"].ToString();
                txt_fname_th.Text = dtUser.Rows[0]["fname_th"].ToString();
                txt_lname_th.Text = dtUser.Rows[0]["lname_th"].ToString();
                txt_id_card.Text = dtUser.Rows[0]["id_card"].ToString();
                txt_birth_date.Text = dtUser.Rows[0]["birth_date"].ToString();
                if (dtUser.Rows[0]["gender"].ToString() == "M")
                {
                    Male.Checked = true;
                }

                txt_Site_id.Text = dtUser.Rows[0]["site_id"].ToString();
                txt_Tag.Text = dtUser.Rows[0]["tag"].ToString();
                txt_Lang.Text = dtUser.Rows[0]["lang"].ToString();

                if (dtUser.Rows[0]["lang"].ToString()=="TH")
                {
                    txt_Lang.Icon = Ext.Net.Icon.FlagTh;
                }else
                {
                    txt_Lang.Icon = Ext.Net.Icon.FlagUs;
                }

                txt_email.Text = dtUser.Rows[0]["email"].ToString();
                txt_password.Text = dtUser.Rows[0]["password"].ToString();
                txt_password_confirm.Text = dtUser.Rows[0]["password"].ToString();
            }        
        }

        protected void getUserCampaign(string user_campaign_id)
        {
            tcdc_trn_user_campaign obj_search = new tcdc_trn_user_campaign();

            obj_search.user_campaign_id = Convert.ToInt32(user_campaign_id);

            DataTable dtCampaign = objUserCampaign.getByObject(obj_search);

            if (dtCampaign != null && dtCampaign.Rows.Count > 0)
            {
                getUserProfile(dtCampaign.Rows[0]["user_id"].ToString());
                txt_Barcode.Text=dtCampaign.Rows[0]["barcode"].ToString();
                txt_RFID.Text = dtCampaign.Rows[0]["RFID"].ToString();
                txt_Expire_date.Text = dtCampaign.Rows[0]["expire_date"].ToString();
            }       
        }

        protected void getAddress(string my_user_id)
        {
            tcdc_mst_address a = new tcdc_mst_address();
            a.user_id = Convert.ToInt32(my_user_id);
            a.address_type = "CONTACT";
            DataTable dtAddress = objAddress.getByObject(a);
            if (dtAddress != null && dtAddress.Rows.Count > 0)
            {
                txt_address_no.Text = dtAddress.Rows[0]["address_no"].ToString();
                txt_province.Text = dtAddress.Rows[0]["province"].ToString();
                txt_country.Text = dtAddress.Rows[0]["country"].ToString();
                txt_post_code.Text = dtAddress.Rows[0]["post_code"].ToString();
            }
        
        }

  
        protected void BrowseImage(object sender, DirectEventArgs e)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "Image Click",
                Message = "คุณคลิกที่รูปสมาชิก"
            });
        }


        protected void StoreUser_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            //StoreUser.DataSource = GetUser();
            //StoreUser.DataBind();
        }     
    }
}